package com.dl.mall.controller;

import cn.dev33.satoken.util.SaResult;
import com.alibaba.fastjson.JSONObject;
import com.dl.mall.annotation.MethodInfo;
import com.dl.mall.config.satoken.StpAdminUtil;
import com.dl.mall.config.satoken.StpUserUtil;
import com.dl.mall.entity.Page;
import com.dl.mall.entity.admin.Admin;
import com.dl.mall.entity.user.User;
import com.dl.mall.mapper.ProductMapper;
import com.dl.mall.service.admin.UserService_Admin;
import com.dl.mall.service.user.UserService;
import com.dl.mall.utils.RedisUtils;
import com.dl.mall.utils.result.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author DL
 * @create 2022-03-12 16:06
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Resource
    private ProductMapper productMapper;

    @Resource
    private UserService_Admin userServiceAdmin;

    @RequestMapping("/sync")
    private void SyncRedisToMysql() {
        Map<Object, Object> productSpecs = redisUtils.hmget("productSpecs");
        System.out.println("productSpecs = " + productSpecs);
        int i = productMapper.updateAllSpecsStock(productSpecs);
        System.out.println("i = " + i);
    }

    @ApiOperation(value="重置用户账号密码为123")
    @GetMapping("/resetPassword")
    private Result resetPassword(String userId) {
        String resetPassword = userServiceAdmin.resetPassword(userId);
        if (resetPassword != null){
            return Result.ok("成功").data("resetPassword", resetPassword);
        }else {
            return Result.error("失败");
        }
    }

    @RequestMapping("/kickout")
    public Result kickout() {
        StpAdminUtil.kickout(StpAdminUtil.getLoginId());
        return Result.ok();
    }

    @RequestMapping("/disable")
    public SaResult disable() {
        // 封禁指定账号
        // 参数一：账号id
        // 参数二：封禁时长，单位：秒  (86400秒=1天，此值为-1时，代表永久封禁)
        StpAdminUtil.disable(StpAdminUtil.getLoginId(), 86400);
        return SaResult.ok();
    }

    @RequestMapping("/disabled")
    public SaResult disabled() {
        //对于正在登录的账号，对其账号封禁时并不会使其立刻注销
        //如果需要将其封禁后立即掉线，可采取先踢再封禁的策略，例如：
        Object loginId = StpAdminUtil.getLoginId();
        StpAdminUtil.kickout(loginId);
        // 封禁指定账号
        // 参数一：账号id
        // 参数二：封禁时长，单位：秒  (86400秒=1天，此值为-1时，代表永久封禁)
        StpAdminUtil.disable(loginId, 86400);
        return SaResult.ok();
    }

    @RequestMapping("/isLogin")
    public Result isLogin() {
        // 获取当前会话是否已经
        // 登录，返回true=已登录，false=未登录
        if (StpAdminUtil.isLogin()){
            return Result.ok();
        }else {
            return Result.error("未登录");
        }
    }

    @Resource
    RedisUtils redisUtils;

    @RequestMapping("/redis/{value}")
    public Result redis(@PathVariable String value) {
        boolean set = redisUtils.set("test", value, 60);
        if (set){
            return Result.ok();
        }else {
            return Result.no("失败");
        }
    }


}
